package com.ycw;

import com.ycw.wach.MyWatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws KeeperException, InterruptedException, IOException {
        Stat stat = new Stat();
        String hostPort = "localhost:2181";
        String path = "/test";
        String value = "ycw";

        ZooKeeper zooKeeper = new ZooKeeper(hostPort, 3000, null);
        MyWatch watch = new MyWatch(zooKeeper);

        Stat exists = zooKeeper.exists(path, watch);
        if (exists == null) {
            String s = zooKeeper.create(path,value.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("zookeeper create success => " + s);
        }

        byte[] data = zooKeeper.getData(path, watch, stat);
        System.out.println("data ==> " + new String(data));

        Thread.sleep(Integer.MAX_VALUE);
    }
}
